<?php
namespace Face\Controller;
use Think\Controller;
class CommonController extends Controller {
    public function __construct()
    {
        parent::__construct();
        
        if(!isset($_SESSION['wechatopenid'])){
            header('Location: http://www.baidu.com/');
        }

        /*
        $op = M('wechat')->find(1);

        $redirecturl= urlencode("http://www.amijie.top/index.php?g=Wechat&m=Wechatuser&a=openid");
        $appid=$op['appid'];
        $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirecturl.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        header("Location:$url");
        */
    }
    /*
    public function index()
    {
        $op = M('wechat')->find(1);
        $code= $_GET['code'];
        $appid= $op['appid'];
        $appsecret= $op['appsecret'];

        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";



        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $data=curl_exec($ch);
        curl_close($ch);
        $jsoninfo=json_decode($data,true);
        $access_token=$jsoninfo['access_token'];

        $_SESSION['access_token'] = $access_token;
        //var_dump($access_token);
        //die();
        $openid = $jsoninfo['openid'];

        $urll = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";

        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$urll);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $data=curl_exec($ch);
        curl_close($ch);
        $jsoninfo=json_decode($data,true);
        $nickname=$jsoninfo['nickname'];
        $openid = $jsoninfo['openid'];
        $headimg = $jsoninfo['headimgurl'];

        $_SESSION['wechatopenid']=$openid;
        $_SESSION['wechatimg']=$headimg;
        $_SESSION['wechatname']=$nickname;

        $data = [
            'name'=>$nickname,
            'openid'=>$openid,
            'headimg'=>$headimg,
            'money'=>2.55,
            'jifen'=>100,
            'yaoqingma'=>rand(0,9).rand(1,9).rand(0,9).rand(0,9),rand(0,9),rand(0,9),
            'lastlogin'=>time(),
        ];
        $save = [
            'name'=>$nickname,
            'openid'=>$openid,
            'headimg'=>$headimg,
        ];
        if(M('wechatuser')->where(['openid'=>$openid])->find()){
            M('wechatuser')->where(['openid'=>$openid])->save($save);
            //$this->redirect('Face/Index/index');
        }else{
            M('wechatuser')->add($data);
            //$this->redirect('Face/Index/index');
        }
    }
    */
}